package com.jiankangjin.weixinchannel.controller;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.weixinchannel.services.impl.RefundServiceImpl;
import com.jiankangjin.weixinchannel.util.StringUtil;
import com.jiankangjin.weixinchannel.vo.RefundVo;

/**
 * 《统一支付渠道》之 退款接口
 */
@Controller
public class RefundController {
	private static final transient Log logger = LogFactory.getLog(RefundController.class);

	@RequestMapping(value="/refund", method=RequestMethod.POST)
	@ResponseBody
	public RefundVo weixinRefund(
			@RequestParam(required = false) String channel_fee_type,
			@RequestParam(required = false) String version,
			@RequestParam(required = false) String pay_ssn_id,
			@RequestParam(required = false) String order_id,
			@RequestParam(required = false) String total_fee,
			@RequestParam(required = false) String pay_chnl_seq,
			@RequestParam(required = false) String refund_fee,
			@RequestParam(required = false) String currency,
			@RequestParam(required = false) String expand_json,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("channel_fee_type=" + channel_fee_type);
		sb.append(" version=" + version);
		sb.append(" pay_ssn_id=" + pay_ssn_id);
		sb.append(" order_id=" + order_id);
		sb.append(" total_fee=" + total_fee);
		sb.append(" pay_chnl_seq=" + pay_chnl_seq);
		sb.append(" refund_fee=" + refund_fee);
		sb.append(" currency=" + currency);
		sb.append(" expand_json=" + expand_json);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("退款接口:" + sb);
		}
		
		RefundVo refundVo = new RefundVo();
		refundVo.setResp_status("0001");
		if (StringUtil.isEmpty(channel_fee_type)) {
			refundVo.setResp_msg("channel_fee_type不能为空");
		}
		if (StringUtil.isEmpty(version)) {
			refundVo.setResp_msg("version不能为空");
		}
		if (StringUtil.isEmpty(pay_ssn_id)) {
			refundVo.setResp_msg("pay_ssn_id不能为空");
		}
		if (StringUtil.isEmpty(order_id)) {
			refundVo.setResp_msg("order_id不能为空");
		}
		if (StringUtil.isEmpty(total_fee)) {
			refundVo.setResp_msg("total_fee不能为空");
		} else {
			if (!StringUtil.isNumer(total_fee)) {
				refundVo.setResp_msg("total_fee必须为数字");
				return refundVo;
			} else {
				if (Integer.parseInt(total_fee) <= 0) {
					refundVo.setResp_msg("total_fee必须大于零");
					return refundVo;
				}
			}
		}
		if (StringUtil.isEmpty(pay_chnl_seq)) {
			refundVo.setResp_msg("pay_chnl_seq不能为空");
		}
		if (StringUtil.isEmpty(refund_fee)) {
			refundVo.setResp_msg("refund_fee不能为空");
		} else {
			if (!StringUtil.isNumer(refund_fee)) {
				refundVo.setResp_msg("refund_fee必须为数字");
				return refundVo;
			} else {
				if (Integer.parseInt(refund_fee) <= 0) {
					refundVo.setResp_msg("refund_fee必须大于零");
					return refundVo;
				}
			}
		}
		if (StringUtil.isEmpty(currency)) {
			refundVo.setResp_msg("currency不能为空");
		}
		if (StringUtil.isEmpty(sign)) {
			refundVo.setResp_msg("sign不能为空");
		}
		if (!channel_fee_type.equalsIgnoreCase("public") && 
				!channel_fee_type.equalsIgnoreCase("barcode") && 
				!channel_fee_type.equalsIgnoreCase("default")) {
			refundVo.setResp_msg("channel_fee_type必须为public barcode default 不区分大小写");
			return refundVo;
		}
		if (!version.equalsIgnoreCase("V0.0.1") && 
				!version.equalsIgnoreCase("V0.1") && 
				!version.equalsIgnoreCase("V1") && 
				!version.equalsIgnoreCase("V2") && 
				!version.equalsIgnoreCase("V3")) {
			refundVo.setResp_msg("version必须类似于0.0.1 V0.1 V1  不区分大小写");
			return refundVo;
		}

		//根据支付类型和版本路由
		try {
			String methodname = channel_fee_type + "_" +version.toUpperCase().replace(".", "_");
			Method transMethod = RefundServiceImpl.class.getMethod(methodname, new Class[]{String.class,String.class,String.class,String.class,String.class,String.class,String.class});
			return (RefundVo) transMethod.invoke(RefundServiceImpl.class.newInstance(), pay_ssn_id,
					order_id, total_fee, pay_chnl_seq, refund_fee, currency, expand_json);
		} catch (Exception e) {
			refundVo.setResp_msg("支付类型和版本路由出错");
			e.printStackTrace();
		}
		return refundVo;
	}
}
